//
// Created by amon.huang on 2021/7/28.
//

#ifndef AMONPLAYER_VIDEO_H
#define AMONPLAYER_VIDEO_H

#include "MediaBase.h"
#include "jni.h"
#include <android/native_window.h>
#include "android/native_window_jni.h"
#include "Audio.h"

#ifdef __cplusplus
extern "C" {
#endif

#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/time.h>

#ifdef __cplusplus
};
#endif

class Video : public MediaBase {
public:
    SwsContext *pSwsContext;
    AVFormatContext *pFormatContext;
    AVStream *pStreams;
    jobject jSurface;

    pthread_t playThreadT;
    pthread_mutex_t seekMutex;

    uint8_t *pFrameBuffer = nullptr;
    int frameSize = 0;
    AVFrame *pRGBAFrame;

    Audio *pAudio;

    double delayTime = 0;
    double defaultDelayTime = 0.04;

    void setNativeSurface(jobject jSurface);

    double getFrameSleepTime(AVFrame *pFrame);

public:
    Video(int audioStreamIndex, JNICall *pJniCall, PlayerStatus *pPlayerStatus, Audio *pAudio);

    ~Video();

    void release();

    void play();

    void pause();

    void seek(uint64_t second);

private:
    virtual void analysisStreamInner(ThreadMode threadMode, AVFormatContext *pFormatContext);
};


#endif //AMONPLAYER_VIDEO_H
